package com.gxa.worker.recycling.controller;

import com.gxa.core.recycling.anno.NeedToken;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.Recycler;
import com.gxa.worker.recycling.service.RecyclerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * @author 廖泓宇
 * @date 2021/12/3 9:48
 */
@CrossOrigin
@RestController
@RequestMapping("/recycler")
@Api("回收员接口")
public class RecyclerController {

    @Autowired
    RecyclerService recyclerService;

    @PostMapping("/updateWorkerPwd")
    @ApiOperation("修改回收员的密码")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "回收员id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "firstPwd", value = "原密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "oldPwd", value = "旧密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "newPwd", value = "新密码", required = true, dataType = "String")
    })
    public Map updateWorkerPwd(@NotNull @RequestParam Long id,@NotNull @RequestParam String firstPwd,@NotNull @RequestParam String phone, @NotNull @RequestParam String oldPwd, @NotNull @RequestParam String newPwd) throws BusinessException {
        recyclerService.updateWorkerPwd(id,firstPwd,phone, oldPwd, newPwd);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    @NeedToken
    @PostMapping("/pwdAndPhone")
    @ApiOperation("根据id查出密码和手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "回收员id", required = true, dataType = "Long")
    })
    public Map updateWorkerPwd(Long id) throws BusinessException {
        Recycler recycler = recyclerService.pwdAndPhone(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(recycler);
        return jsonResult.getDataMap();
    }
}
